﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using V5_DBUtility;
using V5_Model;

using System.Data.OleDb;
namespace V5_DAL.SqlServer {
    public class DALProductOrderProduct {
        #region  BaseMethod
        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId() {
            return DbHelperSQL.GetMaxID("ID", "K_ProductOrderProduct");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from K_ProductOrderProduct");
            strSql.Append(" where ID=" + ID);
            return DbHelperSQL.Exists(strSql.ToString());
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelProductOrderProduct model) {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.ID != null) {
                strSql1.Append("ID,");
                strSql2.Append("" + model.ID + ",");
            }
            if (model.pid != null) {
                strSql1.Append("pid,");
                strSql2.Append("" + model.pid + ",");
            }
            if (model.prototalprice != null) {
                strSql1.Append("prototalprice,");
                strSql2.Append("" + model.prototalprice + ",");
            }
            if (model.promemberprice != null) {
                strSql1.Append("promemberprice,");
                strSql2.Append("" + model.promemberprice + ",");
            }
            if (model.promarketprice != null) {
                strSql1.Append("promarketprice,");
                strSql2.Append("" + model.promarketprice + ",");
            }
            if (model.proquantity != null) {
                strSql1.Append("proquantity,");
                strSql2.Append("" + model.proquantity + ",");
            }
            if (model.profieldvalues != null) {
                strSql1.Append("profieldvalues,");
                strSql2.Append("'" + model.profieldvalues + "',");
            }
            if (model.proname != null) {
                strSql1.Append("proname,");
                strSql2.Append("'" + model.proname + "',");
            }
            if (model.propic != null) {
                strSql1.Append("propic,");
                strSql2.Append("'" + model.propic + "',");
            }
            if (model.proscore != null) {
                strSql1.Append("proscore,");
                strSql2.Append("" + model.proscore + ",");
            }
            if (model.OrderNum != null) {
                strSql1.Append("OrderNum,");
                strSql2.Append("'" + model.OrderNum + "',");
            }
            strSql.Append("insert into K_ProductOrderProduct(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ModelProductOrderProduct model) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update K_ProductOrderProduct set ");
            if (model.pid != null) {
                strSql.Append("pid=" + model.pid + ",");
            }
            if (model.prototalprice != null) {
                strSql.Append("prototalprice=" + model.prototalprice + ",");
            }
            if (model.promemberprice != null) {
                strSql.Append("promemberprice=" + model.promemberprice + ",");
            }
            if (model.promarketprice != null) {
                strSql.Append("promarketprice=" + model.promarketprice + ",");
            }
            if (model.proquantity != null) {
                strSql.Append("proquantity=" + model.proquantity + ",");
            }
            if (model.profieldvalues != null) {
                strSql.Append("profieldvalues='" + model.profieldvalues + "',");
            }
            if (model.proname != null) {
                strSql.Append("proname='" + model.proname + "',");
            }
            if (model.propic != null) {
                strSql.Append("propic='" + model.propic + "',");
            }
            if (model.proscore != null) {
                strSql.Append("proscore=" + model.proscore + ",");
            }
            if (model.OrderNum != null) {
                strSql.Append("OrderNum='" + model.OrderNum + "',");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where ID=" + model.ID + " ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0) {
                return true;
            }
            else {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_ProductOrderProduct ");
            strSql.Append(" where ID=" + ID);
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0) {
                return true;
            }
            else {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelProductOrderProduct GetModel(int ID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from K_ProductOrderProduct ");
            strSql.Append(" where ID=" + ID);
            ModelProductOrderProduct model = new ModelProductOrderProduct();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds != null && ds.Tables[0].Rows.Count > 0) {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "") {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["pid"] != null && ds.Tables[0].Rows[0]["pid"].ToString() != "") {
                    model.pid = int.Parse(ds.Tables[0].Rows[0]["pid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["prototalprice"] != null && ds.Tables[0].Rows[0]["prototalprice"].ToString() != "") {
                    model.prototalprice = int.Parse(ds.Tables[0].Rows[0]["prototalprice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["promemberprice"] != null && ds.Tables[0].Rows[0]["promemberprice"].ToString() != "") {
                    model.promemberprice = int.Parse(ds.Tables[0].Rows[0]["promemberprice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["promarketprice"] != null && ds.Tables[0].Rows[0]["promarketprice"].ToString() != "") {
                    model.promarketprice = int.Parse(ds.Tables[0].Rows[0]["promarketprice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["proquantity"] != null && ds.Tables[0].Rows[0]["proquantity"].ToString() != "") {
                    model.proquantity = int.Parse(ds.Tables[0].Rows[0]["proquantity"].ToString());
                }
                if (ds.Tables[0].Rows[0]["profieldvalues"] != null && ds.Tables[0].Rows[0]["profieldvalues"].ToString() != "") {
                    model.profieldvalues = ds.Tables[0].Rows[0]["profieldvalues"].ToString();
                }
                if (ds.Tables[0].Rows[0]["proname"] != null && ds.Tables[0].Rows[0]["proname"].ToString() != "") {
                    model.proname = ds.Tables[0].Rows[0]["proname"].ToString();
                }
                if (ds.Tables[0].Rows[0]["propic"] != null && ds.Tables[0].Rows[0]["propic"].ToString() != "") {
                    model.propic = ds.Tables[0].Rows[0]["propic"].ToString();
                }
                if (ds.Tables[0].Rows[0]["proscore"] != null && ds.Tables[0].Rows[0]["proscore"].ToString() != "") {
                    model.proscore = int.Parse(ds.Tables[0].Rows[0]["proscore"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OrderNum"] != null && ds.Tables[0].Rows[0]["OrderNum"].ToString() != "") {
                    model.OrderNum = ds.Tables[0].Rows[0]["OrderNum"].ToString();
                }
                return model;
            }
            else {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM K_ProductOrderProduct ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where  1=1 " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion  Method

        #region K_ProductOrderProduct开始
        public bool DeleteList(string IDlist) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_ProductOrderProduct ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0) {
                return true;
            }
            else {
                return false;
            }
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder) {
            return this.GetList("*", Top, strWhere, filedOrder);
        }

        public DataSet GetList(int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            return this.GetList("*", PageSize, PageIndex, strWhere, strOrder, ref RecordCount);
        }

        public List<ModelProductOrderProduct> GetModelList(string strWhere) {
            DataSet ds = this.GetList(strWhere);
            if (ds != null && ds.Tables[0].Rows.Count > 0) {
                return DBCommonHelper.FillModel<ModelProductOrderProduct>(ds);
            }
            return null;
        }

        public List<ModelProductOrderProduct> GetModelList(int Top, string strWhere, string filedOrder) {
            DataSet ds = this.GetList(Top, strWhere, filedOrder);
            if (ds != null && ds.Tables[0].Rows.Count > 0) {
                return DBCommonHelper.FillModel<ModelProductOrderProduct>(ds);
            }
            return null;
        }

        public List<ModelProductOrderProduct> GetModelList(int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            DataSet ds = this.GetList(PageSize, PageIndex, strWhere, strOrder, ref RecordCount);
            if (ds != null && ds.Tables[0].Rows.Count > 0) {
                return DBCommonHelper.FillModel<ModelProductOrderProduct>(ds);
            }
            return null;
        }

        public DataSet GetList(string strFiled, string strWhere) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select " + strFiled + " ");
            strSql.Append(" FROM K_ProductOrderProduct ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where 1=1 " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetList(string strFiled, int Top, string strWhere, string filedOrder) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0) {
                strSql.Append(" Top " + Top.ToString());
            }
            strSql.Append(" " + strFiled + " ");
            strSql.Append(" FROM K_ProductOrderProduct ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where 1=1 " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetList(string strFiled, int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            StringBuilder strSqlCount = new StringBuilder();
            strSqlCount.Append("Select Count(*) as ID From K_ProductOrderProduct ");
            if (strWhere.Trim() != "") {
                strSqlCount.Append(" where 1=1 " + strWhere);
            }
            RecordCount = Convert.ToInt32("0" + DbHelperSQL.GetSingle(strSqlCount.ToString()));
            if (PageIndex <= 1) {
                PageIndex = 1;
            }
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Top " + PageSize + " " + strFiled + " from K_ProductOrderProduct where 1=1 ");
            if (strWhere.Trim() != "") {
                strSql.Append(" " + strWhere);
            }
            if (PageIndex > 1) {
                strSql.Append(" And ID Not In (Select Top " + ((PageIndex - 1) * PageSize) + " ID From K_ProductOrderProduct Where 1=1 ");
                if (strWhere.Trim() != "") {
                    strSql.Append(" " + strWhere);
                }
                if (!string.IsNullOrEmpty(strOrder)) {
                    strSql.Append(" Order by " + strOrder);
                }
                strSql.Append(")");
            }
            if (!string.IsNullOrEmpty(strOrder)) {
                strSql.Append(" Order by " + strOrder);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion K_ProductOrderProduct结束




    }
}
